using System;
using System.Diagnostics;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using newtelligence.DasBlog.Runtime;

namespace newtelligence.DasBlog.Web
{
	/// <summary>
	///		Summary description for LogDateBar.
	/// </summary>
	public class LogDateBar : UserControl
	{
		protected DropDownList DropDownListMonth = new DropDownList();
		protected DropDownList DropDownListDay = new DropDownList();
		protected System.Web.UI.WebControls.Button buttonGo;
		protected DropDownList DropDownListYear = new DropDownList();

		protected System.Resources.ResourceManager resmgr;
		private void Page_Load(object sender, EventArgs e)
		{
			resmgr = ApplicationResourceTable.Get();

			if (!Page.IsPostBack)
			{
				DateTime dt = new DateTime(DateTime.Now.Year, 1, 1);

				DateTime filteredDate = DateTime.Now.ToUniversalTime();

				string date = null;

				if (this.Request.QueryString["date"] != null)
				{
					date = this.Request.QueryString["date"];

					try
					{
						filteredDate = DateTime.ParseExact(date,"yyyy-MM-dd",CultureInfo.InvariantCulture);
					}
					catch (FormatException ex)
					{
						ErrorTrace.Trace(TraceLevel.Error, ex);
					}
				}

				for (int i = 0; i > -12; i--)
				{
					this.DropDownListYear.Items.Add(dt.AddYears(i).ToString("yyyy"));
				}

				this.DropDownListYear.SelectedIndex = this.DropDownListYear.Items.IndexOf(new ListItem(filteredDate.Year.ToString()));

				for (int i = 0; i < 12; i++)
				{
					this.DropDownListMonth.Items.Add(dt.AddMonths(i).ToString("MMMM"));
				}

				SiteConfig siteConfig = SiteConfig.GetSiteConfig();

				if (siteConfig.AdjustDisplayTimeZone && date == null)
				{
					newtelligence.DasBlog.Util.WindowsTimeZone tz = siteConfig.GetConfiguredTimeZone();
					DateTime localTime = tz.ToLocalTime(filteredDate);
					this.DropDownListMonth.SelectedIndex = localTime.Month - 1;
				}
				else
				{
					this.DropDownListMonth.SelectedIndex = filteredDate.Month - 1;
				}			

				int numDays = DateTime.DaysInMonth(Convert.ToInt32(this.DropDownListYear.SelectedItem.Text), this.DropDownListMonth.SelectedIndex + 1);
			
				for (int i = 1; i <= numDays; i++)
				{
					this.DropDownListDay.Items.Add(i.ToString());
				}

				if (siteConfig.AdjustDisplayTimeZone && date == null)
				{
					newtelligence.DasBlog.Util.WindowsTimeZone tz = siteConfig.GetConfiguredTimeZone();
					DateTime localTime = tz.ToLocalTime(filteredDate);
					this.DropDownListDay.SelectedIndex = localTime.Day - 1;
				}
				else
				{
					this.DropDownListDay.SelectedIndex = filteredDate.Day - 1;
				}
			}

			DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.buttonGo.Click += new System.EventHandler(this.buttonGo_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void DropDownChanged()
		{
			string requestUrl = this.Context.Request.Path;
			string month = (this.DropDownListMonth.SelectedIndex + 1).ToString();
			string day = this.DropDownListDay.SelectedItem.Text;

			if (month.Length == 1) month = "0" + month;
			if (day.Length == 1) day = "0" + day;

			string responseUrl = String.Format("{0}?date={1}-{2}-{3}", 
				requestUrl, 
				this.DropDownListYear.SelectedItem,
				month, day);

			this.Context.Response.Redirect(responseUrl);
		}

		private void buttonGo_Click(object sender, System.EventArgs e)
		{
			this.DropDownChanged();
		}
	}
}